package cn.jinjimi.app.presenter;

import cn.jinjimi.base.preference.PreferenceManager;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.CustomerController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.LocalEvent.CustomAllTopEvent;
import cn.jinjimi.app.controller.event.LoginSuccessEvent;
import cn.jinjimi.app.controller.event.NoticeEvent.NoticeEvent;
import cn.jinjimi.app.controller.event.NoticeEvent.NoticeNewClueEvent;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.event.CustomCountEvent;
import cn.jinjimi.app.custom.event.CustomTagGetEvent;
import cn.jinjimi.app.custom.event.CustomTypeEvent;
import cn.jinjimi.app.custom.event.CustomerListChangeEvent;
import cn.jinjimi.app.custom.iview.CustomerListView;
import cn.jinjimi.app.custom.manager.CustomsManager;
import cn.jinjimi.app.interfaces.IView;
import cn.jinjimi.app.managers.NoticeManager;
import cn.jinjimi.app.store.entity.NoticeEntity;
import cn.jinjimi.app.bean.SharePrefKeys;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 所有的客户
 * <p/>
 * Created by jinsen on 15/10/23.
 */
public class CustomerAllPresenter extends NetBasePresenter {

    CustomerListView mView;
    boolean firstRefreshFailFlag = false;

    public CustomerAllPresenter(IView view) {
        super(view);

        mView = (CustomerListView) view;
        mView.loading();
        CustomerController.getCustomTag();
    }

    public void refreshAll() {
        if (AccountManager.getInstance().isLogin()) {
            refreshCustoms();
            NoticeManager.getNoticesByCode(NoticeEntity.TypeNewClue);
        }
    }

    /* 刷新客户列表 */
    public void refreshCustoms() {
        CustomsManager.getInstance().getCustoms();
    }

    public boolean isFirstRefreshFail() {
        return firstRefreshFailFlag;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomerListChangeEvent(CustomerListChangeEvent event) {
        switch (event.state) {
            case CustomerListChangeEvent.StateCacheSuc:
            case CustomerListChangeEvent.StateNetSuc:
                break;
            case CustomerListChangeEvent.StateNetEmpty:
                mView.loadSucc();
                return;
            case CustomerListChangeEvent.StateNetFail:
                firstRefreshFailFlag = true;
                break;
        }
        mView.loadSucc();
        mView.setCustomers(CustomsManager.getInstance().getCustomsList());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomTypeEvent(CustomTypeEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                Custom custom = event.data;
                if (custom != null) {
                    mView.updateTypeSucc(custom.type);
                }
                break;
            case DataEvent.FAIL:
                mView.showToast(R.string.error_bad_network);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginSuccessEvent(LoginSuccessEvent event) {
        if (AccountManager.getInstance().isLogin()) {
            mView.loading();
            CustomsManager.getInstance().initCustoms();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNoticeNewClueEvent(NoticeNewClueEvent event) {
        if (event.state == NoticeEvent.StateAdd) {
            mView.setDrpCount(event.count);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomCountEvent(CustomCountEvent event) {
        mView.setCustomCount();
    }

    /* 监听请求标签接口返回的数据 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomTagGetEvent(CustomTagGetEvent event) {
        switch (event.state) {
            case CustomTagGetEvent.SUCC:
                if (event.data != null && event.data.getData() != null) {
                    PreferenceManager.setData(SharePrefKeys.PREF_CUSTOM_TAGS, GsonUtils.toJson(event.data));
                }
                break;
            case CustomTagGetEvent.FAIL:
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomAllTopEvent(CustomAllTopEvent event) {
        mView.smoothTop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mView = null;
    }
}
